﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;
using SlimDX.Direct3D9;

namespace Gype.UI.D3D9
{
    internal class ArcProvider
    {
        public ArcProvider(Device dev, Rendertarget9 target)
        {
            mDevice = dev;
            mTarget = target;
            mVertexShader = new ShaderCompiler<VertexShader>(dev, Shaders.Arc, "VertexMain", "vs_3_0");
            mPixelShaderDraw = new ShaderCompiler<PixelShader>(dev, Shaders.Arc, "PixelMainDraw", "ps_3_0");

            mDescSizeDraw = mPixelShaderDraw.ConstantTable["elementSize"];
            mDescMatrix = mVertexShader.ConstantTable["orthoProjMatrix"];
            mDescEllipseDraw = mPixelShaderDraw.ConstantTable["ellipseParams"];
            mDescStroke = mPixelShaderDraw.ConstantTable["strokeWidth"];
            mDescArcDraw = mPixelShaderDraw.ConstantTable["arcParams"];
        }

        public void Draw(float a, float b, Vector2 pos, Color4 color, float stroke, float angleMin, float angleMax)
        {
            var vertices = mTarget.CreateTriangleList(pos, new Vector2(2 * a + 6, 2 * b + 6), color);

            mDevice.VertexShader = mVertexShader.Shader;
            mDevice.PixelShader = mPixelShaderDraw.Shader;

            mVertexShader.SetValue(mDescMatrix, mTarget.OrthoMatrix);
            mPixelShaderDraw.SetValue(mDescEllipseDraw, new Vector2(a, b));
            mPixelShaderDraw.SetValue(mDescSizeDraw, new Vector2(2 * a + 6, 2 * b + 6));
            mPixelShaderDraw.SetValue(mDescStroke, stroke);
            mPixelShaderDraw.SetValue(mDescArcDraw, new Vector2((angleMin * (float)Math.PI) / 180.0f, (angleMax * (float)Math.PI) / 180.0f));

            mDevice.VertexDeclaration = mTarget.UIVertexDeclaration;
            mDevice.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices);

            mDevice.VertexShader = null;
            mDevice.PixelShader = null;

        }

        Device mDevice;
        Rendertarget9 mTarget;
        ConstantDescription mDescArcDraw;
        ConstantDescription mDescEllipseDraw;
        ConstantDescription mDescSizeDraw;
        ConstantDescription mDescStroke;
        ConstantDescription mDescMatrix;
        ShaderCompiler<VertexShader> mVertexShader;
        ShaderCompiler<PixelShader> mPixelShaderDraw;
    }
}
